//
//  ZLPushToast.m
//  ZLCocoaKit_Example
//
//  Created by Dev on 2024/12/5.
//  Copyright © 2024 itzhaolei. All rights reserved.
//

#import "ZLPushToast.h"
#import <ZLCocoaKit/ZLItemReuseStackView.h>
#import <ReactiveObjC/ReactiveObjC.h>
#import <Masonry/Masonry.h>

@interface ZLPushToast()

/// 滑动视图
@property (nonatomic, strong) UIScrollView *scrollView;
/// 内容视图
@property (nonatomic, strong) UIView *contentView;
/// 滑动视图
@property (nonatomic, strong) ZLItemReuseStackView<UIView *> *stackView;

/// 垂直对齐方式
@property (nonatomic, unsafe_unretained) ZLPushToastVerticalAlignment alignment;

@end

@implementation ZLPushToast

- (instancetype)initWithAlignment:(ZLPushToastVerticalAlignment)alignment {
    if (self = [super initWithFrame:CGRectZero]) {
        self.alignment = alignment;
        [self addSubviews];
        [self addConstraints];
        [self addActions];
    }
    return self;
}

#pragma mark - Add
- (void)addSubviews {
    [self addSubview:self.scrollView];
    [self.scrollView addSubview:self.contentView];
    [self.contentView addSubview:self.stackView];
}

#pragma mark - Constraint
- (void)addConstraints {
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentInset);
    }];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.width.equalTo(self.scrollView);
        make.height.mas_greaterThanOrEqualTo(self.scrollView);
    }];
    [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (self.alignment == ZLPushToastVerticalAlignmentTop) {
            make.top.equalTo(self.contentView);
            make.bottom.equalTo(self.contentView).priorityLow();
        }else if (self.alignment == ZLPushToastVerticalAlignmentBottom) {
            make.top.equalTo(self.contentView).priorityLow();
            make.bottom.equalTo(self.contentView);
        }else {
            make.centerY.equalTo(self.contentView);
        }
        make.leading.equalTo(self.contentView);
        make.trailing.equalTo(self.contentView);
    }];
}

#pragma mark - Action
- (void)addActions {
    __weak typeof(self)weakSelf = self;
    [RACObserve(self, spacing) subscribeNext:^(id  _Nullable x) {
        weakSelf.stackView.spacing = weakSelf.spacing;
    }];
    [RACObserve(self, contentInset) subscribeNext:^(id  _Nullable x) {
        [weakSelf.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(weakSelf.contentInset);
        }];
    }];
    [[RACObserve(self, stackView.viewCount) skip:1] subscribeNext:^(id  _Nullable x) {
        if (!weakSelf.stackView.viewCount) {
            [weakSelf removeFromSuperview];
        }
    }];
}

+ (ZLPushToast *)push:(UIView *)view inView:(__weak UIView *)superView alignment:(ZLPushToastVerticalAlignment)alignment {
    ZLPushToast *unitView = nil;
    for (UIView *tempView in superView.subviews) {
        if ([tempView isKindOfClass:self.class]) {
            unitView = (ZLPushToast *)tempView;
            break;
        }
    }
    if (!unitView) {
        unitView = [[ZLPushToast alloc] initWithAlignment:alignment];
    }
    [superView addSubview:unitView];
    [unitView.stackView addArrangedSubview:view];
    [unitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(superView);
    }];
    return unitView;
}

#pragma mark - lazy
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        UIScrollView *view = UIScrollView.new;
        view.showsVerticalScrollIndicator = NO;
        //ios11 适配
        if (@available(iOS 11.0, *)) {
            view.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            view.scrollIndicatorInsets = view.contentInset;
        }
        _scrollView = view;
    }
    return _scrollView;
}
- (UIView *)contentView {
    if (!_contentView) {
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.redColor;
        _contentView = view;
    }
    return _contentView;
}
- (ZLItemReuseStackView *)stackView {
    if (!_stackView) {
        ZLItemReuseStackView *view = ZLItemReuseStackView.new;
        view.axis = UILayoutConstraintAxisVertical;
        _stackView = view;
    }
    return _stackView;
}

@end
